<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { queryOtherChargeTableCol } from '@/api/expense/types/expense'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { adjustOtherCharge } from '@/api/expense'
import { getToken } from '@/utils/cache/cookies'
import CustomFormItem from '@/components/basic/CustomFormItem.vue'
const dialogVisible = ref<boolean>(false)
const data = ref<queryOtherChargeTableCol | null>(null)
const previousData = ref<queryOtherChargeTableCol | null>(null)
const open = () => {
  if (data.value?.feeChangeId !== previousData.value?.feeChangeId) {
    previousData.value = data.value
  }
  dialogVisible.value = true
}
const setData = (newData: queryOtherChargeTableCol) => {
  data.value = newData
}
defineExpose({ open, setData })
const emits = defineEmits(['refresh'])
const form = reactive({
  applyAmount: 0,
  reason: ''
})
const formRef = ref<FormInstance | null>(null)
const rules = reactive<FormRules>({
  reason: [
    {
      required: true,
      message: '请输入调整原因',
      trigger: 'blur'
    }
  ],
  applyAmount: [
    {
      required: true,
      message: '请输入减少待支付金额',
      trigger: 'blur'
    },
    {
      type: 'pattern',
      message: '减少待支付金额不能为负数',
      trigger: 'blur',
      pattern: /^(0|[1-9]\d*)(\.\d+)?$/
    }
  ]
})
const cancle = () => {
  dialogVisible.value = false
}
const handleClose = () => {
  formRef.value?.resetFields()
}
const loading = ref<boolean>(false)
const confirm = () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const res = await adjustOtherCharge({
          token: getToken() ?? '',
          feeChangeId: data.value?.feeChangeId ?? '',
          ...form
        })
        if (res.statusCode === '0') {
          ElMessage.success(res.data)
          emits('refresh')
          dialogVisible.value = false
        }
        loading.value = false
      } catch (error) {
        console.error(error)
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="待付调整"
    width="750px"
    class="adjust-fee-dialog"
    @close="handleClose"
  >
    <el-form :model="form" ref="formRef" :rules="rules" label-width="150px">
      <custom-form-item :label-width="150">
        <template #label>
          <span>用户手机号:</span>
        </template>
        <template #value>
          <span>{{ data?.phone }}</span>
        </template>
        <template #label2>
          <span>车辆编号:</span>
        </template>
        <template #value2>
          <span>{{ data?.bikeCode }}</span>
        </template>
      </custom-form-item>
      <custom-form-item :label-width="150">
        <template #label>
          <span>关联订单/行程编号:</span>
        </template>
        <template #value>
          <span>{{ data?.orderId }}</span>
        </template>
      </custom-form-item>
      <custom-form-item :label-width="150">
        <template #label>
          <span>收费类型:</span>
        </template>
        <template #value>
          <span>{{ data?.operationType }}</span>
        </template>
        <template #label2>
          <span>收费时间:</span>
        </template>
        <template #value2>
          <span>{{ data?.changeTime }}</span>
        </template>
      </custom-form-item>
      <custom-form-item :label-width="150">
        <template #label>
          <span>收费金额:</span>
        </template>
        <template #value>
          <span>{{ data?.amount }}</span>
        </template>
        <template #label2>
          <span>支付状态:</span>
        </template>
        <template #value2>
          <span
            :style="{
              color:
                data?.payStatus === '已支付' ? 'var(--xy-success-color)' : 'var(--xy-warning-color)'
            }"
            >{{ data?.payStatus }}</span
          >
        </template>
      </custom-form-item>
      <custom-form-item :label-width="150">
        <template #label>
          <span>相关图片:</span>
        </template>
        <template #value>
          <div v-if="data?.imageUrls">
            <el-image
              v-for="(img, index) in data?.imageUrls.split(',')"
              :key="index"
              :src="img"
              :preview-src-list="data?.imageUrls.split(',')"
              w-50
              h-50
              :preview-teleported="true"
            />
          </div>
        </template>
      </custom-form-item>

      <el-form-item prop="applyAmount" label="减少待支付金额">
        <el-input v-model="form.applyAmount" type="number">
          <template #prepend>￥</template>
        </el-input>
        <span class="text-[12px] text-[#999999] leading-17 mt-5"
          >只支持对未支付的行程费、违规费进行调整，调整后用户支付剩余金额即可</span
        >
      </el-form-item>
      <el-form-item prop="reason" label="原因">
        <el-input v-model="form.reason" :min="2" placeholder="请输入原因" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button plain @click="cancle">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped></style>
